package com.atguigui.leetcode1;

import java.util.HashMap;
import java.util.Map;

/**
 * 1124.表现良好的最长时间段
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/2/14 9:07
 */
public class P1124LongestWellPerformingInterval {

    public static void main(String[] args) {
        Solution solution = new P1124LongestWellPerformingInterval().new Solution();
        int[] hours = {9, 9, 6, 0, 6, 6, 9};
        solution.longestWPI(hours);
        // TO TEST
    }

    class Solution {
        public int longestWPI(int[] hours) {
            int n = hours.length;
            //存放第几天工作不劳累的
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            int s = 0, res = 0;
            for (int i = 0; i < n; i++) {
                //累计的
                s += hours[i] > 8 ? 1 : -1;
                if (s > 0) {
                    res = Math.max(res, i + 1); //工作劳累的
                } else {
                    //
                    if (map.containsKey(s - 1)) {
                        res = Math.max(res, i - map.get(s - 1));
                    }
                }
                if (!map.containsKey(s)) {
                    map.put(s, i);
                }
            }

            return res;
        }
    }
}
